package springbook.user.test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.either;
import static org.hamcrest.CoreMatchers.nullValue;

import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import java.util.HashSet;
import java.util.Set;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/springbook/user/test/junit.xml"})
public class JUnitTest1 {
	
	@Autowired
	ApplicationContext context;
	
	static Set<JUnitTest1> testObjects = new HashSet<JUnitTest1>();
	static ApplicationContext contextObject = null;

	
	@Test
	public void test() {
		System.out.println(context);
	}
	@Test
	public void test1() {
		assertThat(testObjects, not(hasItem(this)));
		testObjects.add(this);
		
		System.out.println("test1 => contextObject ======> " + contextObject);
		System.out.println("test1 => this.context  ======> " + this.context );
		
		assertThat(contextObject == null || contextObject == this.context, is(true));
		
		contextObject = this.context;	
	}

	@Test
	public void test2() {
		assertThat(testObjects, not(hasItem(this)));
		testObjects.add(this);
		
		System.out.println("test2 => contextObject ======> " + contextObject);
		System.out.println("test2 => this.context  ======> " + this.context );

		assertTrue(contextObject == null || contextObject == this.context);
		contextObject = this.context;
	}
	
	@Test
	public void test3() {
		assertThat(testObjects, not(hasItem(this)));
		testObjects.add(this);

		System.out.println("test3 => contextObject ======> " + contextObject);
		System.out.println("test3 => this.context  ======> " + this.context );

		assertThat(contextObject, either(is(nullValue())).or(is(this.context)));
		
		contextObject = this.context;		
	}
	
	@Test
	public void test4() {
		int i = 0;
		for(JUnitTest1 testObject: testObjects) {
			System.out.println("===================> " + (i++) + "--------->>>>" + testObject);
		}
	}

}
